import Vue from 'vue'
import VueRouter from 'vue-router'


//导入进度条 插件
import Nprogress from "nprogress"
import "nprogress/nprogress.css"


//配置进度条插件( 关闭 右侧的圆圈动画 )
Nprogress.configure({ showSpinner: false });


Vue.use(VueRouter)

const routes = [
  {
    path: '/myshouye',
    name: 'myshouye',
    meta: { title: '首页' },
    component: () => import('../views/Myshouye.vue'),

    children: [
      {
        path: '/myshouye/shouye',
        name: 'shouye',
        meta: { title: '首页' },
        component: () => import('../views/shouye/shouye.vue')
      },
      {
        path: '/myshouye/baobaotuan',
        name: 'baobaotuan',
        meta: { title: '爆爆团' },
        component: () => import('../views/shouye/baobaotuan.vue')
      },
      {
        path: '/myshouye/dingdan',
        name: 'dingdan',
        meta: { title: '订单' },

        component: () => import('../views/shouye/dingdan.vue')
      },
      {
        path: '/myshouye/wode',
        name: 'wode',
        meta: { title: '我的' },

        component: () => import('../views/shouye/wode.vue')
      },
      {
        path: '/myshouye',
        redirect: '/myshouye/shouye'
      },
      //二级路由的重定向
      {
        path: '/myshouye',
        redirect: '/myshouye/shouye'
      },
      //404路由
      {
        path: '*',
        component: () => import('../views/NotFound.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '登陆' },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta: { title: '注册' },
    component: () => import('../views/Register.vue')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    meta: { title: '详情页' },
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/collect',
    name: 'collect',
    meta: { title: '收藏页' },
    component: () => import('../views/Collect.vue')
  },
  {
    path: '/search',
    name: 'search',
    meta: { title: '搜索页' },
    component: () => import('../views/Search.vue')
  },
  {
    path: '/searchgoods',
    name: 'searchgoods',
    meta: { title: '搜索页' },
    component: () => import('../views/SearchGoods.vue')
  },
  {
    path: '/address',
    name: 'address',
    meta: { title: '地址页' },
    component: () => import('../views/Address.vue')
  },
  {
    path: '/addaddress',
    name: 'addaddress',
    meta: { title: '地址新增页' },
    component: () => import('../views/Addaddress.vue')
  },
  {
    path: '/reviseaddress/:item',
    name: 'reviseaddress',
    meta: { title: '修改地址页' },
    component: () => import('../views/Reviseaddress.vue')
  },

  //重定向路由
  {
    path: '/',
    redirect: '/myshouye'
  },
  //404路由
  {
    path: '*',
    component: () => import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {

  Nprogress.start()
  //参数一: to 新的路由对象
  //参数二: from 旧的路由对象
  //参数三: next 路由控制方法, 调用该方法则允许路由跳转, 未调用该方法则不允许路由跳转

  //动态更新网页标题
  document.title = to.meta.title;

  //获取localStorage中的登陆凭证
  var token = localStorage.getItem('token');

  if (to.path == '/register') { //跳往 注册页 , 允许跳转
    next();
  } else {
    if (to.path == '/login') { //跳往 登录页
      if (token) { //已登录 , 重定向到 "/"
        next('/');
      } else { //未登录 , 允许跳往 登录页
        next();
      }
      // next();
    } else { //跳往 非登录页
      if (token) { //已登录 , 允许访问
        next();
      } else { //未登录 , 重定向到 "/login"
        next('/login');
      }
    }
  }
})
//路由的全局后置守卫方法
router.afterEach((to, from) => {
  // console.log('afterEach');
  Nprogress.done();

})

export default router